<template>
    <div class="container">
      <div class="toptitle">
        <div class="titletext">操作记录</div>
        <el-divider />
      </div>
  
      <!-- 标签栏 -->
      <el-tabs v-model="activeTab" @tab-click="fetchData" class="custom-tabs">
        <el-tab-pane label="控制柜" name="控制柜"></el-tab-pane>
        <el-tab-pane label="水肥机" name="水肥机"></el-tab-pane>
      </el-tabs>
  
      <!-- 筛选器 -->
      <div class="filters">
        <el-select
          v-model="selectedDevice"
          placeholder="请选择设备"
          @change="fetchData"
          style="width: 200px; margin-right: 10px"
        >
          <el-option
            v-for="device in devices"
            :key="device.id"
            :label="device.name"
            :value="device.id"
          />
        </el-select>
  
        <el-select
          v-model="selectedChannel"
          placeholder="请选择通道"
          @change="fetchData"
          style="width: 200px;"
        >
          <el-option
            v-for="channel in channels"
            :key="channel.id"
            :label="channel.name"
            :value="channel.id"
          />
        </el-select>
      </div>
  
      <!-- 数据表 -->
      <el-table
        :data="filteredRecords"
        style="width: 100%; margin-top: 20px"
        border
      >
        <el-table-column prop="controlCabinet" label="控制柜" />
        <el-table-column prop="landBlock" label="所属地块" />
        <el-table-column prop="device" label="设备" />
        <el-table-column prop="channel" label="通道" />
        <el-table-column prop="user" label="人员" />
        <el-table-column prop="operation" label="操作" />
        <el-table-column prop="category" label="类别" />
        <el-table-column prop="status" label="状态" />
        <el-table-column prop="note" label="备注" />
        <el-table-column prop="time" label="时间" />
      </el-table>
  
      <!-- 分页 -->
      <el-pagination
        style="margin-top: 20px; text-align: center"
        :current-page="currentPage"
        :page-size="pageSize"
        :total="totalRecords"
        @current-change="changePage"
        layout="prev, pager, next, total"
      />
    </div>
  </template>
  
  <script setup>
  import { ref, computed } from "vue";
  
  const devices = ref([
    { id: "1", name: "设备1" },
    { id: "2", name: "设备2" },
    { id: "3", name: "设备3" },
  ]);
  
  const channels = ref([
    { id: "1", name: "通道1" },
    { id: "2", name: "通道2" },
    { id: "3", name: "通道3" },
  ]);
  
  const allRecords = ref([
    {
      id: 1,
      controlCabinet: "A柜",
      landBlock: "地块1",
      device: "设备1",
      channel: "通道1",
      user: "张三",
      operation: "操作描述1",
      category: "类别1",
      status: "正常",
      note: "无",
      time: "2024-12-19 10:00:00",
    },
    {
      id: 2,
      controlCabinet: "B柜",
      landBlock: "地块2",
      device: "设备2",
      channel: "通道2",
      user: "李四",
      operation: "操作描述2",
      category: "类别2",
      status: "异常",
      note: "注意事项",
      time: "2024-12-19 11:00:00",
    },
    {
      id: 3,
      controlCabinet: "C柜",
      landBlock: "地块3",
      device: "设备3",
      channel: "通道3",
      user: "王五",
      operation: "操作描述3",
      category: "类别3",
      status: "正常",
      note: "无",
      time: "2024-12-19 12:00:00",
    },
  ]);
  
  const activeTab = ref("控制柜");
  
  const selectedDevice = ref("");
  const selectedChannel = ref("");
  

  const currentPage = ref(1);
  const pageSize = ref(10);
  
  
  const filteredRecords = computed(() => {
    let filtered = allRecords.value;
  
    // 按设备筛选
    if (selectedDevice.value) {
      filtered = filtered.filter(
        (record) => record.device === devices.value.find((d) => d.id === selectedDevice.value)?.name
      );
    }
  
    // 按通道筛选
    if (selectedChannel.value) {
      filtered = filtered.filter(
        (record) => record.channel === channels.value.find((c) => c.id === selectedChannel.value)?.name
      );
    }
  
    // 分页
    const start = (currentPage.value - 1) * pageSize.value;
    const end = start + pageSize.value;
    return filtered.slice(start, end);
  });
  
  // 总记录数
  const totalRecords = computed(() => {
    let filtered = allRecords.value;
  
    if (selectedDevice.value) {
      filtered = filtered.filter(
        (record) => record.device === devices.value.find((d) => d.id === selectedDevice.value)?.name
      );
    }
  
    if (selectedChannel.value) {
      filtered = filtered.filter(
        (record) => record.channel === channels.value.find((c) => c.id === selectedChannel.value)?.name
      );
    }
  
    return filtered.length;
  });
  const changePage = (page) => {
    currentPage.value = page;
  };
  
  // 模拟获取数据
  const fetchData = () => {
    console.log("获取数据，当前选项卡：", activeTab.value);
  };
  </script>
  
  <style lang="scss" scoped>
    .container {
        flex: 1;
        border-radius: 10px;
        box-sizing: border-box;
        display: flex;
        flex-direction: column;
        padding: 10px;
  
    .toptitle {
      margin-bottom: 0px;
  
      .titletext {
        border-left: 3px solid var(--primary-color);
        padding-left: 10px;
        font-size: 18px;
        font-weight: bold;
      }
  
      hr {
        background-color: rgb(194, 194, 194);
        height: 1px;
        border: none;
        margin: 5px 0;
      }
    }
    .custom-tabs {
        margin-top: -20px;
        background-color: #ffffff;
        // border: 1px solid #dcdfe6; 
        border-radius: 8px;
        padding: 10px; /* 内边距 */
        ::v-deep(.el-tabs__item) {
            font-size: 20px !important;
            color:var(--text-color);
            margin: 0 15px;
            padding: 5px 10px;
            border-radius: 4px; 
            transition: all 0.3s; 
        }
        ::v-deep(.el-tabs__item.is-active) {
            color: var(--primary-color);
            font-weight: bold; 
        }
        ::v-deep(.el-tabs__item:hover) {
            color: var(--primary-color);
        }
        ::v-deep(.el-tabs__active-bar) {
            background-color: var(--primary-color);
            height: 3px;
            border-radius: 2px; 
            transition: all 0.3s;
            
            
        }

    }
    ::v-deep(.el-table__header) {
        background-color: #f4f4f4;
        color: var(--primary-color); 
        font-weight: bold;
        font-size: 16px;
        text-align: center; 
        border-bottom: 2px solid #ddd; 
    }

    .el-table {
        width: 100%;
        border: 1px solid #ddd; 
        border-radius: 8px;
        box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
    }
    
  
    .filters {
      display: flex;
      margin-bottom: 20px;
    }
  }
  </style>
  